CLASS zcl_moon_unmanaged_query DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_rap_query_provider.
  PROTECTED SECTION.
  PRIVATE SECTION.
    TYPES:
        tt_tb TYPE STANDARD TABLE OF zrap_nwf_996t.

    METHODS get_data_from_request
      IMPORTING io_request TYPE REF TO if_rap_query_request
      EXPORTING rt_result  TYPE tt_tb
      RAISING   cx_rap_query_filter_no_range.
ENDCLASS.



CLASS ZCL_MOON_UNMANAGED_QUERY IMPLEMENTATION.


  METHOD get_data_from_request.
    TYPES:
      tt_r_ebeln TYPE RANGE OF zrap_nwf_996t-ebeln,
      tt_r_matnr TYPE RANGE OF zrap_nwf_996t-matnr.

    DATA lt_r_key  TYPE tt_r_ebeln.
    DATA lt_r_text TYPE tt_r_matnr.

    DATA(lt_parameters) = io_request->get_parameters( ).
    DATA(lt_filter) = io_request->get_filter( )->get_as_ranges( ).
    DATA(ld_offset) = io_request->get_paging( )->get_offset( ).
    DATA(ld_pagesize) = io_request->get_paging( )->get_page_size( ).

    LOOP AT lt_filter INTO DATA(ls_filter).
      CASE ls_filter-name.
        WHEN 'Ebeln'.
          lt_r_key = CORRESPONDING #( ls_filter-range ).
        WHEN 'Matnr'.
          lt_r_text = CORRESPONDING #( ls_filter-range ).

      ENDCASE.
    ENDLOOP.

*    SELECT FROM zrap_nwf_996t
*          FIELDS *
*          WHERE ebeln IN @lt_r_key
*            AND matnr IN @lt_r_text
*          INTO TABLE @rt_result.


*    SELECT FROM I_PurchaseOrderAPI01
*    FIELDS PurchaseOrder
*    INTO TABLE @DATA(LT_ITAB)
*    UP TO 10 ROWS.

    SELECT FROM YB_I_PurchaseOrderAPI01
    FIELDS PurchaseOrder
    WHERE PurchaseOrder =  '4500000002'
    INTO TABLE @DATA(LT_ITAB)
    UP TO 10 ROWS.

*    select from I_ReservationDocumentHeader
*    fields Reservation
*    into table @DATA(rtable).
*
*    loop at rtable INTO data(ls_rline).
*    APPEND VALUE #( EBELN = ls_rline-Reservation ) TO rt_result.
*    ENDLOOP.


    LOOP AT LT_ITAB INTO DATA(LS_ITAB).
        APPEND VALUE #( EBELN = LS_ITAB-PurchaseOrder ) TO rt_result.
    ENDLOOP.

  ENDMETHOD.


  METHOD if_rap_query_provider~select.
    DATA lt_output TYPE STANDARD TABLE OF zrap_nwf_996t.
    DATA lt_database TYPE tt_tb.

    DATA:lt_tab TYPE TABLE OF zrap_nwf_996t.
*    lt_tab = VALUE #( ( ebeln = '71' matnr = 'F' ) )   .
*    MODIFY zrap_nwf_996t FROM TABLE @lt_tab.

*    MODIFY zrap_nwf_996t FROM TABLE @lt_tab.


    get_data_from_request( EXPORTING io_request = io_request IMPORTING rt_result = lt_database ).

    lt_output = CORRESPONDING #( lt_database MAPPING ebeln = ebeln matnr = matnr ).

    clear lt_output[].
    APPEND VALUE #( matnr = 'GG' ) TO lt_output.
    IF io_request->is_data_requested( ).
      io_response->set_data( lt_output ).
    ENDIF.

    IF io_request->is_total_numb_of_rec_requested( ).
      io_response->set_total_number_of_records( lines( lt_output ) ).
    ENDIF.
  ENDMETHOD.
ENDCLASS.
